<script setup lang="ts">
import {useRolesStore} from "@/store/rolesStroe.ts";
import {ElNotification, ElTree, FormInstance} from 'element-plus'
import {createRole} from "@/api/role";
import {reqAddRoleData} from "@/api/role/type.ts";


const treeRef = ref<InstanceType<typeof ElTree>>()
const loading = ref(false)
const addRoleFormRef = ref<FormInstance>()
const roleStore = useRolesStore();
const addRole = reactive<reqAddRoleData>({
  name: '',
  describe: '',
  isUsable: 1,
  menuIDs: [],

})
// 提交表单
const submitEdit = async (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      addRole.menuIDs.length = 0
      addRole.menuIDs.push(...treeRef.value!.getCheckedKeys(), ...treeRef.value!.getHalfCheckedKeys())
      const res = await createRole(addRole) //要改
      if (res.code === 200) {
        loading.value = false
        ElNotification({
          message: res.msg,
          type: 'success',
        })
        roleStore.openAdd = false
        await roleStore.getRoleListFun()
      } else {
        loading.value = false
        ElNotification({
          message: res.msg,
          type: 'error',
        })
      }
    } else {
      loading.value = false
      console.log('error submit!', fields)
    }
  })
}
// 表单验证规则
const rules = reactive({
  name: [
    {required: true, message: '请输入角色名称', trigger: 'blur'},
  ],
  describe: [
    {required: true, message: '请输入角色描述', trigger: 'blur'},
  ]
})


</script>

<template>
  <div>
    <el-drawer v-model="roleStore.openAdd" title="编辑角色">
      <el-form :model="addRole" ref="addRoleFormRef" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRole.name"/>
        </el-form-item>
        <el-form-item label="角色描述" prop="describe">
          <el-input v-model="addRole.describe"/>
        </el-form-item>
        <el-form-item label="角色状态">
          <el-radio v-model="addRole.isUsable" :label="1">启用</el-radio>
          <el-radio v-model="addRole.isUsable" :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item label="菜单权限">
          <el-tree style="width: 100%"
                   ref="treeRef"
                   :data="roleStore.menuList"
                   show-checkbox
                   node-key="ID"
                   :props="{
              label: 'name',
              children: 'children',
            }"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="flex: auto">
          <el-button @click="roleStore.openAdd = false">取消</el-button>
          <el-button type="primary" @click="submitEdit(addRoleFormRef)" :loading="loading">确定</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<style lang="scss">
.buttons-right {
  display: flex;
  justify-content: flex-end;
  /* 根据需要，您还可以添加其他样式，比如间距 */
  gap: 8px; /* 使用 gap 属性添加按钮之间的间距（请注意，gap 属性在旧版浏览器中可能需要前缀或使用其他方法实现）*/
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>